package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.nameSpace.classifier.structuredClassifier.Class.behavior;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.uml2.common.edit.command.SubsetAddCommand;
import org.eclipse.uml2.common.edit.command.SubsetSupersetReplaceCommand;
import org.eclipse.uml2.common.edit.command.SupersetRemoveCommand;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;

public class ActivityItemProvider
		extends BehaviorItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public ActivityItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addStructuredNodePropertyDescriptor(object);
			addVariablePropertyDescriptor(object);
			addNodePropertyDescriptor(object);
			addIsReadOnlyPropertyDescriptor(object);
			addEdgePropertyDescriptor(object);
			addPartitionPropertyDescriptor(object);
			addIsSingleExecutionPropertyDescriptor(object);
			addGroupPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	protected void addStructuredNodePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Activity_structuredNode_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Activity_structuredNode_feature", "_UI_Activity_type"),
				UMLPackage.Literals.ACTIVITY__STRUCTURED_NODE, false, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addVariablePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Activity_variable_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Activity_variable_feature", "_UI_Activity_type"),
				UMLPackage.Literals.ACTIVITY__VARIABLE, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addNodePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Activity_node_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Activity_node_feature", "_UI_Activity_type"),
				UMLPackage.Literals.ACTIVITY__NODE, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addIsReadOnlyPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Activity_isReadOnly_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Activity_isReadOnly_feature", "_UI_Activity_type"),
				UMLPackage.Literals.ACTIVITY__IS_READ_ONLY, true, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, null));
	}

	protected void addEdgePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Activity_edge_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Activity_edge_feature", "_UI_Activity_type"),
				UMLPackage.Literals.ACTIVITY__EDGE, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addPartitionPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Activity_partition_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Activity_partition_feature", "_UI_Activity_type"),
				UMLPackage.Literals.ACTIVITY__PARTITION, true, null, null, null));
	}

	protected void addIsSingleExecutionPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Activity_isSingleExecution_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Activity_isSingleExecution_feature", "_UI_Activity_type"),
				UMLPackage.Literals.ACTIVITY__IS_SINGLE_EXECUTION, true, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, null));
	}

	protected void addGroupPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Activity_group_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Activity_group_feature", "_UI_Activity_type"),
				UMLPackage.Literals.ACTIVITY__GROUP, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	@Override
	public Collection getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures.add(UMLPackage.Literals.ACTIVITY__VARIABLE);
			childrenFeatures.add(UMLPackage.Literals.ACTIVITY__NODE);
			childrenFeatures.add(UMLPackage.Literals.ACTIVITY__EDGE);
			childrenFeatures.add(UMLPackage.Literals.ACTIVITY__GROUP);
		}
		return childrenFeatures;
	}

	@Override
	protected EStructuralFeature getChildFeature(Object object, Object child) {

		return super.getChildFeature(object, child);
	}

	@Override
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/Activity"));
	}

	@Override
	public String getText(Object object) {
		return appendLabel(appendType(appendKeywords(new StringBuffer(), object), "_UI_Activity_type"), object).toString();
	}

	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(Activity.class)) {
			case UMLPackage.ACTIVITY__IS_READ_ONLY:
			case UMLPackage.ACTIVITY__IS_SINGLE_EXECUTION:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
			case UMLPackage.ACTIVITY__VARIABLE:
			case UMLPackage.ACTIVITY__NODE:
			case UMLPackage.ACTIVITY__EDGE:
			case UMLPackage.ACTIVITY__GROUP:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	@Override
	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ACTIVITY__NODE, UMLFactory.eINSTANCE.createCallOperationAction()));

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ACTIVITY__NODE, UMLFactory.eINSTANCE.createInitialNode()));

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ACTIVITY__NODE, UMLFactory.eINSTANCE.createForkNode()));

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ACTIVITY__NODE, UMLFactory.eINSTANCE.createFlowFinalNode()));

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ACTIVITY__NODE, UMLFactory.eINSTANCE.createJoinNode()));

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ACTIVITY__EDGE, UMLFactory.eINSTANCE.createControlFlow()));

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ACTIVITY__NODE, UMLFactory.eINSTANCE.createCallBehaviorAction()));

	}

	@Override
	public String getCreateChildText(Object owner, Object feature, Object child, Collection selection) {
		final Object childFeature = feature;
		final Object childObject = child;

		final boolean qualify = childFeature == UMLPackage.Literals.NAMESPACE__OWNED_RULE || childFeature == UMLPackage.Literals.BEHAVIOR__PRECONDITION || childFeature == UMLPackage.Literals.BEHAVIOR__POSTCONDITION || childFeature == UMLPackage.Literals.CLASSIFIER__OWNED_USE_CASE
				|| childFeature == UMLPackage.Literals.CLASS__NESTED_CLASSIFIER || childFeature == UMLPackage.Literals.BEHAVIORED_CLASSIFIER__OWNED_BEHAVIOR || childFeature == UMLPackage.Literals.ACTIVITY__NODE || childFeature == UMLPackage.Literals.ACTIVITY__GROUP;

		if (qualify) {
			return getString("_UI_CreateChild_text2",
					new Object[] { getTypeText(childObject), getFeatureText(childFeature), getTypeText(owner) });
		}
		return super.getCreateChildText(owner, feature, child, selection);
	}

	@Override
	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

	@Override
	protected Command createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection, int index) {
		if (feature == UMLPackage.Literals.ACTIVITY__PARTITION) {
			return new SubsetAddCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ACTIVITY__GROUP }, collection, index);
		}
		return super.createAddCommand(domain, owner, feature, collection, index);
	}

	@Override
	protected Command createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection) {
		if (feature == UMLPackage.Literals.ACTIVITY__GROUP) {
			return new SupersetRemoveCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ACTIVITY__PARTITION }, collection);
		}
		return super.createRemoveCommand(domain, owner, feature, collection);
	}

	@Override
	protected Command createReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, EObject value, Collection collection) {
		if (feature == UMLPackage.Literals.ACTIVITY__PARTITION) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ACTIVITY__GROUP }, null, value, collection);
		}
		if (feature == UMLPackage.Literals.ACTIVITY__GROUP) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, null, new EStructuralFeature[] { UMLPackage.Literals.ACTIVITY__PARTITION }, value, collection);
		}
		return super.createReplaceCommand(domain, owner, feature, value, collection);
	}

}
